package com.zw.pdm.module.designtask.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.module.designtask.service.DesignTaskAddService;
import com.zw.pdm.module.report.model.Project;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Description: 添加下达设计任务
 * <p>
 * 权限：
 * dsn:task:view 查看
 * dsn:task:config 分配下达
 * @Author: China.wsg
 * @Date: 2020/10/13 13:20
 */
@Controller
@RequestMapping("/designTask/add")
@SuppressWarnings("all")
public class DesignTaskAddController extends BaseController {

    @Autowired
    private DesignTaskAddService designTaskAddService;

    @GetMapping("/task")
    @RequiresPermissions({"dsn:task:view"})
    public String taskPage(Model model) {
        model.addAttribute("pid11", designTaskAddService.findDeptByPid(11));
        model.addAttribute("pid12", designTaskAddService.findDeptByPid(12));
        model.addAttribute("pid14", designTaskAddService.findDeptByPid(14));
        model.addAttribute("pid15", designTaskAddService.findDeptByPid(15));
        model.addAttribute("pid13", designTaskAddService.findDeptByPid(13));
        return "designtask/add/taskPage";
    }

    /**
     * 待分配项目列表
     */
    @ResponseBody
    @PostMapping("/taskList")
    @RequiresPermissions({"dsn:task:view"})
    public JsonResult taskList(Project project) {
        startPage();
        List<Project> list = designTaskAddService.findProjectsByQuery(project);
        return jsonResult(list);
    }

    /**
     * 下达任务单
     */
    @ResponseBody
    @PostMapping("/distribute")
    @RequiresPermissions({"dsn:task:config"})
    public JsonResult distribute(@RequestParam("projectId") Integer projectId, @RequestParam("sectionId") Integer[] sectionId) {
        return jsonResult(designTaskAddService.distribute(projectId, sectionId));
    }

    /**
     * 对项目看板下达任务接口
     */
    @ResponseBody
    @PostMapping("/design/distribute")
    @CrossOrigin
    public boolean designInterface(Integer projectId, String[] sectionName) {
        Integer[] sectionId = designTaskAddService.transformId(sectionName);
        return designTaskAddService.distribute(projectId, sectionId);
    }

    /**
     * 项目看板生产项目下达接口
     */
    @ResponseBody
    @PostMapping("/produce/distribute")
    @CrossOrigin
    public boolean produceInterface(Integer projectId,String proCode,String proName) {
        return designTaskAddService.produceDistribute(projectId,proCode,proName);
    }

    /**
     * 项目类型变更接口
     *
     * @param projectId      项目主键
     * @param changeType     0只调科室 1变更成设计 2变更成生产
     * @param befSectionName 调整前科室
     * @param aftSectionName 调整后科室
     * @return
     */
    @ResponseBody
    @PostMapping("/design/taskChange")
    @CrossOrigin
    public boolean taskChangeInterface(Integer projectId, Integer changeType, String[] befSectionName, String[] aftSectionName) {
        return designTaskAddService.taskChangeInterface(projectId, changeType, befSectionName, aftSectionName);
    }


}
